package com.tbs.parkingsaleapp.UI;

import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.tbs.parkingsaleapp.DataTool.RecommendationCalculator;
import com.tbs.parkingsaleapp.Http.Actionner.ADResulter;
import com.tbs.parkingsaleapp.Http.JsonData.DCompany;
import com.tbs.parkingsaleapp.Http.JsonData.DLocation;
import com.tbs.parkingsaleapp.Http.JsonData.LocationInfo;
import com.tbs.parkingsaleapp.Http.JsonData.ParkResult;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.tbs.parkingsaleapp.UI.Listenners.BuyClickListenner;
import com.tbs.parkingsaleapp.UI.Widget.TraitShowController;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;

public class ParkDetailActivity extends AppCompatActivity {

    public  static final String PARK_LOCATION = "ParkLocation",PARK_NUMBER="ParkID";

    public static void ActivityStart(Context context, int location, int pid)
    {
        Intent intent=new Intent();
        intent.putExtra(PARK_LOCATION,location);
        intent.putExtra(PARK_NUMBER,pid);
        intent.setClass(context,ParkDetailActivity.class);
        context.startActivity(intent);
    }
    
    ParkResult park=null;

    void initData()
    {
        int l= getIntent().getIntExtra(PARK_LOCATION,-1);
        int p=getIntent().getIntExtra(PARK_NUMBER,-1);
        for (ParkResult r:SystemDataMananger.FindAllParks(l)
        ) {
            if(r.getPark().getId()==p)
            {
                park=r;
                break;
            }
        }
        if(park==null)
            finish();
    }
    void initView()
    {
        LinearLayout linearLayout=findViewById(R.id.detail_layout);
        ScrollView scrollView=findViewById(R.id.detial_father);

        TraitShowController traitShowController=new TraitShowController(linearLayout,park.getTrait());
        LocationInfo location =SystemDataMananger.FindLocation(park.getPark().getLocation());
        TextView title=findViewById(R.id.detial_title);
        title.setText(String.format("%s  %d号车位",location.getLocation().getName(),park.getPark().getNumber()));
        Banner banner=findViewById(R.id.detial_banner);
        ArrayList<String> imgs=new ArrayList<>();
//                ArrayList<String> titles=new ArrayList<>();
        for (String s:park.getImgPaths()
        ) {
            imgs.add(s);
            //titles.add(total.get(position).getTrait().toString());
        }
        banner.setImageLoader(new ADResulter.myImgLoader());
        banner.setImages(imgs);
//                banner.setBannerTitles(titles);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setBannerAnimation(Transformer.CubeOut);
        banner.isAutoPlay(true);
        banner.setDelayTime(10000);
        banner.start();

        TextView textView=findViewById(R.id.detial_location);
        textView.setText(String.format("地址：%s",location.getLocation().getAddress()));
        textView=findViewById(R.id.detial_company);
        DCompany company= location.getCompany().get(0);
        String type="";
        switch (company.getType())
        {
            case 1:
                type="开发商";
                break;
            case 2:
                type="承包商";
                break;
            case 0:
                type="其他";
                break;
        }
        textView.setText(String.format("%s\n类型:%s\n公司介绍:%s\n联系电话:%s\nEmail:%s",company.getName(),type,company.getAbout(),company.getPhone(),company.getEmail()
                ));
        textView=findViewById(R.id.detial_note);
        textView.setText(String.format("备注：%s",park.getPark().getNote()));
        textView=findViewById(R.id.detial_info);
        String sta="";
        String[] texts={"车位已售出","车位已预定","车位在售"};
        String st=texts[park.getPark().getStatus()+1];
        textView.setText(String.format("已下单人数:%d\n车位与您的喜好符合数:%d\n与您所拥有的小区最近的距离:%.2f\n车位状态:%s",park.getOrderedCount(),park.applyCount(), RecommendationCalculator.fromUser(location.getLocation()),st));
        textView=findViewById(R.id.detial_price);
        textView.setText(String.format("价格:%.2f 元",park.getPark().getPrice()));
        Button button=findViewById(R.id.detail_order);
        if(park.getPark().getStatus()!=1)
        {
            button.setText("不可下单");
            button.setBackgroundColor(getColor(R.color.banBuy));
        }
        else
            button.setBackground(getDrawable(R.drawable.buttonstyle));
        button.setTag(park);
        button.setOnClickListener(new BuyClickListenner());

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_park_detail);
        initData();
        initView();
    }
}